//
//  STDSHairColorEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSHairColorEffect_h
#define STDSHairColorEffect_h

#include "STDSBaseEffect.hpp"

class STDSHairColorEffect: public STDSBaseEffect {
public:
    STDSHairColorEffect() {
        mEffectType = STDSEffectTypeHairColor;
    }
    ~STDSHairColorEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setStringArtuments(const char *name, const char *stringValue) override {
        if (stringValue != nullptr) {
            mHairPath = string(stringValue);
            mHairColorTexture = nullptr;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mHairColorProgram == nullptr) {
            mHairColorProgram = mContext->getProgram(kShaderSRCHairColor);
        }
        return mHairColorProgram;
    }
    
    shared_ptr<STDSTexture> getHairColorTexture() {
        if (mHairColorTexture == nullptr) {
            mHairColorTexture = mContext->getImageTexture(mHairPath);
        }
        return mHairColorTexture;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        auto hairTex = getHairColorTexture();
        if (hairTex == nullptr || mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture"), 0);
        
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, hairTex->getId());
        program->setUniform(program->getUniform("hairColorTexture"), 1);
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    string mHairPath{""};
    shared_ptr<STDSTexture> mHairColorTexture{nullptr};
    shared_ptr<STDSShaderProgram> mHairColorProgram{nullptr};
};
#endif /* STDSHairColorEffect_h */
